<?php

declare(strict_types=1);

namespace DreamCat\Array2Class\TransImpl;

use DreamCat\Array2Class\PropertyNameTransInterface;
use JetBrains\PhpStorm\Pure;

/**
 * 下划线转驼峰
 * @author vijay
 */
class UnderLine2CamelCase implements PropertyNameTransInterface
{
    /** @var string 匹配正则 */
    private const PREG = "#_[A-Za-z]#";

    /**
     * @inheritDoc
     * @noinspection PhpPureFunctionMayProduceSideEffectsInspection
     */
    #[Pure]
    public function convert(string $propertyName): string
    {
        return preg_replace_callback(
            self::PREG,
            [
                $this,
                "replace",
            ],
            $propertyName
        );
    }

    /**
     * 替换函数
     * @param string[] $match 正则匹配到的部分
     * @return string 要替换的部分
     */
    #[Pure]
    private function replace(array $match): string
    {
        return strtoupper(substr($match[0], 1));
    }
}

# end of file
